<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Albums
 * @description : Process albums, photos, videos
 * 
 */
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Album extends AUTH_Controller {

  /**
   * Setting PHP Limits
   * 
   */
  public function __construct() {
	parent::__construct();
	ini_set('memory_limit', '250M');
	ini_set('upload_max_filesize', '250M');
	ini_set('post_max_size', '250M');
	ini_set('max_input_time', 3600);
	ini_set('max_execution_time', 3600);
  }

  /**
   * List photos and videos of an album
   * 
   */
  public function index($album_id = NULL) {

	// Loading text helper
	$this->load->helper('text');

	// Checking album id
	if (!empty($album_id) && is_numeric($album_id)) {

	  // Init variable
	  $album_data = NULL;

	  // Getting album details
	  $get_album = $this->album_model->get_album($album_id);

	  // Checking album details
	  if ($get_album['status'] == 1 && !empty($get_album['result'])) {

		$album_data = $get_album['result'];

		// Private album only visible to owner
		if ($album_data['album_status'] == 2 && $this->current_user['user_id'] != $album_data['album_user_id']) {
		  redirect('albums');
		  return;
		}

		// Fetch album photos to album details
		$get_photos = $this->album_model->get_photos($album_id);
		if ($get_photos['status'] == 1 && !empty($get_photos['result'])) {
		  $album_data['album_photos'] = $get_photos['result'];
		}

		// Getting recent comments on photos of album
		$album_photos_comments = array();
		$get_album_photo_comments = $this->comment_model->get_album_photo_comments($album_id);
		if ($get_album_photo_comments['status'] == 1 && !empty($get_album_photo_comments['result'])) {
		  $album_photos_comments = $get_album_photo_comments['result'];
		}

		// Preparing & loading view
		$view_data = array();
		$view_data['menu_active'] = 'albums';
		$view_data['album_id'] = $album_id;
		$view_data['album_data'] = $album_data;
		$view_data['album_photos_comments'] = $album_photos_comments;
		$this->load_view('album', $view_data);
		return;
	  }
	}

	// Redirect if something wrong
	redirect('albums');
	return;
  }

  /**
   * Create new album and upload photos and videos
   * 
   */
  public function create() {

	// Init variables
	$new_album_status = NULL;
	$album_name = NULL;
	$err_msg = NULL;

	// Checking request type
	if ($this->is_post()) {

	  $image_upload = array();
	  $new_album_status = 3;

	  // Getting album details
	  $album_name = $this->sanitize($this->input->post('album_name', TRUE));
	  $album_status = $this->sanitize($this->input->post('album_status', TRUE));
	  $album_status = ($album_status) ? $album_status : 2;

	  // Checking inputs
	  if (!empty($album_name) && strlen($album_name) <= 250 && !empty($_FILES) && is_array($_FILES)) {

		// Prepraing upload config & upload files
		$config = array();
		$config['upload_path'] = 'assets/temp/';
		$config['allowed_types'] = 'gif|jpg|png|mpg|mpeg|avi|mp4|3g2|3gp|flv';
		$config['max_size'] = '256000';
		$this->load->library('upload');
		$this->upload->initialize($config);
		$this->upload->do_multi_upload("album_image");
		$image_upload = $this->upload->get_multi_upload_data();
		$err_msg = $this->upload->display_errors();

		if (!empty($image_upload) && is_array($image_upload)) {

		  // Creating album
		  $set_album = $this->album_model->set_album($album_name, $this->current_user['user_id'], $album_status);
		  $album_id = $set_album['result']['album_id'];

		  // Creating album directory
		  @mkdir('assets/albums/' . $album_id . '/', 0777, TRUE);

		  $count = 0;
		  $this->load->library('image_lib');

		  // Processing album photos and videos
		  foreach ($image_upload as $image_data) {

			$rand_string = md5(microtime() . rand(11111, 99999));

			// Saving images to album
			if (($image_data['file_type'] == 'image/jpeg') || ($image_data['file_type'] == 'image/gif') || ($image_data['file_type'] == 'image/png')) {
			  $image_file_name = $rand_string . '.jpg';
			  $thumb_file_name = $rand_string . '_thumb.jpg';
			  $new_image_file = 'assets/albums/' . $album_id . '/' . $image_file_name;
			  $new_thumb_file = 'assets/albums/' . $album_id . '/' . $thumb_file_name;
			  $this->create_image($image_data['full_path'], $new_image_file);
			  $this->create_thumb($image_data['full_path'], $new_thumb_file, $image_data['image_width'], $image_data['image_height']);
			  if (file_exists($new_image_file)) {
				$count++;
				$this->album_model->add_photo($album_id, $image_file_name, 1);
			  }
			}

			// Saving video to album
			else {

			  $video_file_name = $rand_string . '.flv';
			  $video_thumb_file_name = $rand_string . '_thumb.jpg';
			  $new_video_file = 'assets/albums/' . $album_id . '/' . $video_file_name;
			  $new_video_thumb_file = 'assets/albums/' . $album_id . '/' . $video_thumb_file_name;
			  $this->create_video($image_data['full_path'], $new_video_file);
			  $this->create_video_thumb($image_data['full_path'], $new_video_thumb_file);
			  if (file_exists($new_video_file)) {
				$count++;
				$this->album_model->add_photo($album_id, $video_file_name, 2);
			  }
			}

			// Remove temp file
			@unlink($image_data['full_path']);
		  }

		  // Setting status values
		  $new_album_status = 1;
		  $this->session->set_flashdata('new_album_status', $new_album_status);
		  $this->session->set_flashdata('album_album_id', $album_id);
		  $this->session->set_flashdata('album_album_hash', md5($album_id . 'album'));

		  // Redirect to newly created album
		  redirect('album/' . $album_id);
		  return;
		}
	  }

	  // Setting session & Redirect
	  $this->session->set_flashdata('new_album_msg', $err_msg);
	  $this->session->set_flashdata('new_album_status', $new_album_status);
	  redirect('album/create');
	  return;
	}

	// Preparing view data
	$view_data = array();
	$view_data['menu_active'] = 'albums';
	$view_data['album_name'] = $album_name;
	$this->load_view('album_create', $view_data);
	return;
  }

  /**
   * Create the image file on the given location from an image.
   * Also resize the new image
   * 
   */
  private function create_image($orig_file, $new_file) {
	$config = array();
	$config['image_library'] = 'gd2';
	$config['source_image'] = $orig_file;
	$config['maintain_ratio'] = TRUE;
	$config['new_image'] = $new_file;
	$config['width'] = 1024;
	$config['height'] = 768;
	$this->image_lib->clear();
	$this->image_lib->initialize($config);
	$this->image_lib->resize();
  }

  /**
   * Create a square thumb of the image
   * 
   */
  private function create_thumb($orig_file, $new_file, $orig_width, $orig_height) {

	// Image croping
	if ($orig_width > $orig_height) {
	  $new_width = $orig_height;
	  $new_height = $orig_height;
	  $x = @round(($orig_width / 2) - ($new_width / 2));
	  $y = 0;
	} elseif ($orig_height > $orig_width) {
	  $new_width = $orig_width;
	  $new_height = $orig_width;
	  $x = 0;
	  $y = @round(($orig_height / 2) - ($new_height / 2));
	} else {
	  $new_width = $orig_width;
	  $new_height = $orig_height;
	  $x = 0;
	  $y = 0;
	}

	// Croping
	$config = array();
	$config['image_library'] = 'gd2';
	$config['source_image'] = $orig_file;
	$config['new_image'] = $new_file;
	$config['width'] = $new_width;
	$config['height'] = $new_height;
	$config['x_axis'] = $x;
	$config['y_axis'] = $y;
	$this->image_lib->clear();
	$this->image_lib->initialize($config);
	$this->image_lib->crop();

	// Resizing
	$config = array();
	$config['image_library'] = 'gd2';
	$config['source_image'] = $new_file;
	$config['new_image'] = $new_file;
	$config['maintain_ratio'] = FALSE;
	$config['width'] = 320;
	$config['height'] = 240;
	$this->image_lib->clear();
	$this->image_lib->initialize($config);
	$this->image_lib->resize();
  }

  /**
   * Create a new flv video file from the provied video file (Required ffmpeg)
   * 
   */
  private function create_video($input_file = NULL, $output_file = NULL) {
	$command_video = "ffmpeg -i " . escapeshellarg((string) $input_file) . " -f flv -vf 'scale=1024:-1' -ar 22050 -b 1024k " . escapeshellarg((string) $output_file) . " &";
	system($command_video);
	if (file_exists($output_file)) {
	  return TRUE;
	} else {
	  return FALSE;
	}
  }

  /**
   * Create thumb image of the flv video file (Required ffmpeg)
   * 
   */
  private function create_video_thumb($input_file = NULL, $output_file = NULL) {

	if (!file_exists($output_file)) {
	  $this->load->library('ffmpeg');
	  $this->ffmpeg->clear()->input($input_file)->thumb('320x240', 1, 10)->output($output_file)->ready();
	}

	if (!file_exists($output_file)) {
	  $command_thumb = "ffmpeg -i " . escapeshellarg((string) $input_file) . " -deinterlace -an -ss 1 -t 00:00:05 -r 1 -y -s 320x240 -vcodec mjpeg -f mjpeg " . escapeshellarg((string) $output_file) . " 2>&1";
	  system($command_thumb);
	}

	if (!file_exists($output_file)) {
	  $command_thumb = "ffmpeg -i " . escapeshellarg((string) $input_file) . " -deinterlace -an -ss 1 -f mjpeg -t 1 -r 1 -y -s 320x240 " . escapeshellarg((string) $output_file) . " 2>&1";
	  system($command_thumb);
	}

	if (!file_exists($output_file)) {
	  $command_thumb = "ffmpeg -itsoffset -105 -i " . escapeshellarg((string) $input_file) . " -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 " . escapeshellarg((string) $output_file) . " 2>&1";
	  system($command_thumb);
	}

	if (file_exists($output_file)) {
	  return TRUE;
	} else {
	  return FALSE;
	}
  }

  /**
   * Update an album.
   * Process uploading new photos and videos to album, rename album, delete photos and videos from album
   * Change type of album, etc.
   * 
   */
  public function update($album_id = NULL, $req_type = NULL) {

	// Init variables
	$update_album_status = NULL;
	$album_name = NULL;

	// Checking album id
	if (!empty($album_id) && is_numeric($album_id)) {

	  // Getting album details
	  $get_album = $this->album_model->get_album($album_id);

	  // Checking album details
	  if ($get_album['status'] == 1 && !empty($get_album['result'])) {

		// Getting album photos and injecting to album main array
		$get_photos = $this->album_model->get_photos($album_id);
		if ($get_photos['status'] == 1 && !empty($get_photos['result'])) {
		  $get_album['result']['album_photos'] = $get_photos['result'];
		}

		$album_data = $get_album['result'];

		// Checking permssion (Owner or Admin)
		if (($album_data['album_user_id'] == $this->current_user['user_id']) || (($this->is_admin))) {

		  if ($this->is_post()) {

			$update_album_status = 2;

			// Process album updating
			if ($req_type == 'upd') {

			  // Getting inputs
			  $image_upload = array();
			  $album_name = $this->sanitize($this->input->post('album_name', TRUE));
			  $album_status = $this->sanitize($this->input->post('album_status', TRUE));
			  $album_status = ($album_status) ? $album_status : 2;

			  // Checking album name
			  if (!empty($album_name) && strlen($album_name) <= 250) {

				// Updating album details
				$update_album = $this->album_model->update_album($album_id, $album_name, $album_status);

				// Getting image or video files
				if (!empty($_FILES) && is_array($_FILES)) {

				  // Setting upload config
				  $config = array();
				  $config['upload_path'] = 'assets/temp/';
				  $config['allowed_types'] = 'gif|jpg|png|mpg|mpeg|avi|mp4|3g2|3gp|flv';
				  $config['max_size'] = '256000';
				  $this->load->library('upload');
				  $this->upload->initialize($config);
				  $this->upload->do_multi_upload("album_image");
				  $image_upload = $this->upload->get_multi_upload_data();
				  $err_msg = $this->upload->display_errors();

				  // Checking file upload
				  if (!empty($image_upload) && is_array($image_upload)) {

					// Make sure album folder exits
					@mkdir('assets/albums/' . $album_id . '/', 0777, TRUE);
					$this->load->library('image_lib');
					$count = 0;

					// Processing album photos and videos
					foreach ($image_upload as $image_data) {

					  $rand_string = md5(microtime() . rand(11111, 99999));

					  // Saving images to album
					  if (($image_data['file_type'] == 'image/jpeg') || ($image_data['file_type'] == 'image/gif') || ($image_data['file_type'] == 'image/png')) {
						$image_file_name = $rand_string . '.jpg';
						$thumb_file_name = $rand_string . '_thumb.jpg';
						$new_image_file = 'assets/albums/' . $album_id . '/' . $image_file_name;
						$new_thumb_file = 'assets/albums/' . $album_id . '/' . $thumb_file_name;
						$this->create_image($image_data['full_path'], $new_image_file);
						$this->create_thumb($image_data['full_path'], $new_thumb_file, $image_data['image_width'], $image_data['image_height']);
						if (file_exists($new_image_file)) {
						  $count++;
						  $this->album_model->add_photo($album_id, $image_file_name, 1);
						}
					  }

					  // Saving video to album
					  else {

						$video_file_name = $rand_string . '.flv';
						$video_thumb_file_name = $rand_string . '_thumb.jpg';
						$new_video_file = 'assets/albums/' . $album_id . '/' . $video_file_name;
						$new_video_thumb_file = 'assets/albums/' . $album_id . '/' . $video_thumb_file_name;
						$this->create_video($image_data['full_path'], $new_video_file);
						$this->create_video_thumb($image_data['full_path'], $new_video_thumb_file);
						if (file_exists($new_video_file)) {
						  $count++;
						  $this->album_model->add_photo($album_id, $video_file_name, 2);
						}
					  }

					  // Remove temp file
					  @unlink($image_data['full_path']);
					}

					// Setting update status
					if (!empty($count)) {
					  $update_album_status = 1;
					}

					// Redirect to album
					$this->session->set_flashdata('update_album_status', $update_album_status);
					redirect('album/' . $album_id);
					return;
				  }
				}

				// Redirect to album page
				if ($update_album['status'] == 1 && !empty($update_album['result'])) {
				  $update_album_status = 1;
				  $this->session->set_flashdata('update_album_status', $update_album_status);
				  redirect('album/' . $album_id);
				  return;
				}
			  }

			  // Redirect to album update page
			  $this->session->set_flashdata('update_album_msg', $err_msg);
			  $this->session->set_flashdata('update_album_status', $update_album_status);
			  redirect('album/update/' . $album_id . '/upd');
			  return;
			}

			// Process album photos delete
			if ($req_type == 'del') {
			  $delete_photo_ids = $this->input->post('delete_photo_ids', TRUE);
			  if (!empty($delete_photo_ids) && is_array($delete_photo_ids)) {
				foreach ($delete_photo_ids as $delete_photo_id) {
				  $get_photo = $this->album_model->get_photo($delete_photo_id, $album_id);
				  if ($get_photo['status'] == 1 && !empty($get_photo['result'])) {
					$image_file = 'assets/albums/' . $album_id . '/' . $get_photo['result']['photo_file'];
					$thumb_file = 'assets/albums/' . $album_id . '/' . substr($get_photo['result']['photo_file'], 0, 32) . '_thumb.jpg';
					if ($get_photo['result']['photo_video_type'] == 2) {
					  $this->like_model->del_likes_by_post('video', $get_photo['result']['photo_id']);
					  $this->comment_model->del_comment_by_post('video', $get_photo['result']['photo_id']);
					} else {
					  $this->like_model->del_likes_by_post('photo', $get_photo['result']['photo_id']);
					  $this->comment_model->del_comment_by_post('photo', $get_photo['result']['photo_id']);
					}
					$delete_photo = $this->album_model->delete_photo($delete_photo_id, $album_id);
					if ($delete_photo['status'] == 1 && !empty($delete_photo['result'])) {
					  @unlink($image_file);
					  @unlink($thumb_file);
					}
				  }
				}
				$update_album_status = 1;
			  }
			}

			// Redirect to album
			$this->session->set_flashdata('update_album_status', $update_album_status);
			redirect('album/' . $album_id);
			return;
		  }

		  // Load view
		  $view_data = array();
		  $view_data['menu_active'] = 'albums';
		  $view_data['album_id'] = $album_id;
		  $view_data['album_data'] = $album_data;
		  $this->load_view('album_update', $view_data);
		  return;
		}
	  }
	}

	redirect('albums');
  }

  /**
   * View a photo of given id
   * 
   */
  public function photo($photo_id = NULL) {

	// Checking photo id
	if (!empty($photo_id) && is_numeric($photo_id)) {

	  // Getting photo details (type 1)
	  $get_photo_details = $this->album_model->get_photo_details($photo_id, 1);

	  // Checking photo details
	  if ($get_photo_details['status'] == 1 && !empty($get_photo_details['result'])) {

		$photo_data = $get_photo_details['result'];

		// Private album photo only visible to owner
		if ($photo_data['album_status'] == 2 && $this->current_user['user_id'] != $photo_data['album_user_id']) {
		  redirect('albums');
		  return;
		}

		// Getting previous photo id
		$prev_id = NULL;
		$prev_type = NULL;
		$get_prev_photo_id = $this->album_model->get_prev_photo_id($photo_id, $photo_data['album_id']);
		if ($get_prev_photo_id['status'] == 1 && !empty($get_prev_photo_id['result'])) {
		  $prev_id = $get_prev_photo_id['result']['photo_id'];
		  $prev_type = $get_prev_photo_id['result']['photo_video_type'];
		}

		// Getting next photo id
		$next_id = NULL;
		$next_type = NULL;
		$get_next_photo_id = $this->album_model->get_next_photo_id($photo_id, $photo_data['album_id']);
		if ($get_next_photo_id['status'] == 1 && !empty($get_next_photo_id['result'])) {
		  $next_id = $get_next_photo_id['result']['photo_id'];
		  $next_type = $get_next_photo_id['result']['photo_video_type'];
		}

		// Getting photo comments
		$comment_type = 'photo';
		$comment_type_id = $photo_id;
		$comments_data = array();
		$total_comments_count = 0;
		$comments_limit = $this->input->get('limit', TRUE);
		$comments_limit = (is_numeric($comments_limit)) ? $comments_limit : 10;
		$get_comments = $this->comment_model->get_comments($comment_type, $comment_type_id, $comments_limit);
		$get_comments_count = $this->comment_model->get_comments($comment_type, $comment_type_id, NULL);
		if ($get_comments['status'] == 1 && !empty($get_comments['result'])) {
		  $comments_data = $get_comments['result'];
		  $total_comments_count = $get_comments_count['count'];
		}

		// Preparing view
		$view_data = array();
		$view_data['menu_active'] = 'albums';
		$view_data['photo_id'] = $photo_id;
		$view_data['photo_data'] = $photo_data;
		$view_data['prev_id'] = $prev_id;
		$view_data['prev_type'] = $prev_type;
		$view_data['next_id'] = $next_id;
		$view_data['next_type'] = $next_type;
		$view_data['comment_type'] = $comment_type;
		$view_data['comment_type_id'] = $comment_type_id;
		$view_data['comments_data'] = $comments_data;
		$view_data['total_comments_count'] = $total_comments_count;
		$view_data['comments_limit'] = $comments_limit;
		$this->load_view('album_photo', $view_data);
		return;
	  }
	}

	// Redirect if something wrong
	redirect();
  }

  /**
   * View a video of given id
   * 
   */
  public function video($video_id = NULL) {

	if (!empty($video_id) && is_numeric($video_id)) {

	  // Getting video details (type 2)
	  $get_video_details = $this->album_model->get_photo_details($video_id, 2);

	  if ($get_video_details['status'] == 1 && !empty($get_video_details['result'])) {

		$photo_data = $get_video_details['result'];

		// Private album photo only visible to owner
		if ($photo_data['album_status'] == 2 && $this->current_user['user_id'] != $photo_data['album_user_id']) {
		  redirect('albums');
		  return;
		}

		// Getting previous photo id
		$prev_id = NULL;
		$prev_type = NULL;
		$get_prev_photo_id = $this->album_model->get_prev_photo_id($video_id, $photo_data['album_id']);
		if ($get_prev_photo_id['status'] == 1 && !empty($get_prev_photo_id['result'])) {
		  $prev_id = $get_prev_photo_id['result']['photo_id'];
		  $prev_type = $get_prev_photo_id['result']['photo_video_type'];
		}

		// Getting next photo id
		$next_id = NULL;
		$next_type = NULL;
		$get_next_photo_id = $this->album_model->get_next_photo_id($video_id, $photo_data['album_id']);
		if ($get_next_photo_id['status'] == 1 && !empty($get_next_photo_id['result'])) {
		  $next_id = $get_next_photo_id['result']['photo_id'];
		  $next_type = $get_next_photo_id['result']['photo_video_type'];
		}

		// Get video comments
		$comment_type = 'video';
		$comment_type_id = $video_id;
		$comments_data = array();
		$total_comments_count = 0;
		$comments_limit = $this->input->get('limit', TRUE);
		$comments_limit = (is_numeric($comments_limit)) ? $comments_limit : 10;
		$get_comments = $this->comment_model->get_comments($comment_type, $comment_type_id, $comments_limit);
		$get_comments_count = $this->comment_model->get_comments($comment_type, $comment_type_id, NULL);
		if ($get_comments['status'] == 1 && !empty($get_comments['result'])) {
		  $comments_data = $get_comments['result'];
		  $total_comments_count = $get_comments_count['count'];
		}

		// Preparing view
		$view_data = array();
		$view_data['menu_active'] = 'albums';
		$view_data['photo_id'] = $video_id;
		$view_data['photo_data'] = $photo_data;
		$view_data['prev_id'] = $prev_id;
		$view_data['prev_type'] = $prev_type;
		$view_data['next_id'] = $next_id;
		$view_data['next_type'] = $next_type;
		$view_data['comment_type'] = $comment_type;
		$view_data['comment_type_id'] = $comment_type_id;
		$view_data['comments_data'] = $comments_data;
		$view_data['total_comments_count'] = $total_comments_count;
		$view_data['comments_limit'] = $comments_limit;
		$this->load_view('album_video', $view_data);
		return;
	  }
	}

	// Redirect if something wrong
	redirect();
  }

  /**
   * Add or Update description of a photo or video of album
   * 
   */
  public function photo_description() {
	$photo_edit_status = 2;
	$photo_video_type = 0;
	$photo_id = $this->input->post('photo_id', TRUE);
	$photo_description = $this->sanitize($this->input->post('photo_description', TRUE));
	if (!empty($photo_id) && is_numeric($photo_id) && !empty($photo_description)) {
	  $get_photo_details = $this->album_model->get_photo_details($photo_id);
	  if ($get_photo_details['status'] == 1 && !empty($get_photo_details['result'])) {
		$photo_data = $get_photo_details['result'];
		if (($this->current_user['user_id'] == $photo_data['album_user_id']) || ($this->is_admin)) {
		  if ($photo_data['photo_video_type'] == 1) {
			$photo_video_type = 1;
		  }
		  if ($photo_data['photo_video_type'] == 2) {
			$photo_video_type = 2;
		  }
		  $update_photo_desc = $this->album_model->update_photo_desc($photo_id, $photo_description);
		  if ($update_photo_desc['status'] == 1 && !empty($update_photo_desc['result'])) {

			// Set taggin notificaion
			$mentioned_users = get_mentioned_users($photo_description);
			if (!empty($mentioned_users)) {
			  foreach ($mentioned_users as $mentioned_user) {
				if ($mentioned_user['user_id'] != $this->current_user['user_id']) {
				  if ($photo_video_type == 2) {
					$post_id = 'VO' . $photo_id;
					$toast_text = '@' . $this->current_user['user_name'] . ' tagged you in a video <b>#' . $post_id . '</b>';
				  } else {
					$post_id = 'PO' . $photo_id;
					$toast_text = '@' . $this->current_user['user_name'] . ' tagged you in a photo <b>#' . $post_id . '</b>';
				  }
				  @add_toast_notificaion($mentioned_user['user_id'], $toast_text, $post_id);
				}
			  }
			}

			$photo_edit_status = 1;
		  }
		}
	  }
	}
	$this->session->set_flashdata('photo_edit_status', $photo_edit_status);
	if ($photo_video_type == 1) {
	  redirect('album/photo/' . $photo_id);
	} else if ($photo_video_type == 2) {
	  redirect('album/video/' . $photo_id);
	} else {
	  redirect();
	}
  }

  /**
   * Delete an album
   * 
   */
  public function delete($album_id = NULL) {
	$delete_album_status = 2;
	if (!empty($album_id) && is_numeric($album_id)) {
	  $get_album = $this->album_model->get_album($album_id);
	  if ($get_album['status'] == 1 && !empty($get_album['result'])) {
		$album_data = $get_album['result'];
		if (($album_data['album_user_id'] == $this->current_user['user_id']) || (($this->is_admin))) {

		  // Delete likes/comments of album photos / videos
		  $get_photos = $this->album_model->get_photos($album_id);
		  $this->like_model->del_likes_by_post('album', $album_id);
		  if ($get_photos['status'] == 1 && !empty($get_photos['result'])) {
			foreach ($get_photos['result'] as $photo) {
			  $this->like_model->del_likes_by_post('photo', $photo['photo_id']);
			  $this->like_model->del_likes_by_post('video', $photo['photo_id']);
			  $this->comment_model->del_comment_by_post('photo', $photo['photo_id']);
			  $this->comment_model->del_comment_by_post('video', $photo['photo_id']);
			}
		  }

		  // Delete album files
		  $album_folder = 'assets/albums/' . $album_id . '/';
		  @$this->delete_folder($album_folder);
		  $delete_album = $this->album_model->delete_album($album_id);
		  if ($delete_album['status'] == 1 && !empty($delete_album['result'])) {
			$delete_album_status = 1;
		  }
		}
	  }
	}
	$this->session->set_flashdata('delete_album_status', $delete_album_status);
	redirect('albums');
	return;
  }

  /**
   * Function to help album deletion
   * Delete enitre folder and its files
   * 
   */
  private function delete_folder($path) {
	if (is_dir($path) === true) {
	  $files = array_diff(scandir($path), array('.', '..'));
	  foreach ($files as $file) {
		$this->delete_folder(realpath($path) . '/' . $file);
	  }
	  return rmdir($path);
	} else if (is_file($path) === true) {
	  return unlink($path);
	}
	return false;
  }

  /**
   * Function to get file type of a file
   * 
   */
  private function get_mime($file) {
	if (function_exists("finfo_file")) {
	  $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
	  $mime = finfo_file($finfo, $file);
	  finfo_close($finfo);
	  return $mime;
	} else if (function_exists("mime_content_type")) {
	  return mime_content_type($file);
	} else if (!stristr(ini_get("disable_functions"), "shell_exec")) {
	  $file = escapeshellarg($file);
	  $mime = shell_exec("file -bi " . $file);
	  return $mime;
	} else {
	  return false;
	}
  }

  /**
   * (=== Not public ===)
   * 
   * Add a new video to album by providing the album id and file path
   * Convert the video & add to album.
   * Used to process large videos directly by developer.
   * 
   */
  public function add_video_to_album() {

	$video_path = '/media/disk1/ISFF/w2/125 - I am Sorry - Work Station 2.mov';
	$album_id = '130';

	if (!empty($video_path) && file_exists($video_path)) {
	  $rand_string = md5(microtime() . rand(11111, 99999));
	  $video_file_name = $rand_string . '.flv';
	  $video_thumb_file_name = $rand_string . '_thumb.jpg';
	  $new_video_file = 'assets/albums/' . $album_id . '/' . $video_file_name;
	  $new_video_thumb_file = 'assets/albums/' . $album_id . '/' . $video_thumb_file_name;
	  $this->create_video($video_path, $new_video_file);
	  $this->create_video_thumb($video_path, $new_video_thumb_file);
	  if (file_exists($new_video_file)) {
		$this->album_model->add_photo($album_id, $video_file_name, 2);
		die('Success');
	  }
	  die('Error : No video created');
	}
	die('Error : File not found');
  }

  /**
   * (=== Not public ===)
   * 
   * Insert a dummy album entry for video
   * 
   */
  public function add_dummy_album_item($album_id = 0, $video_file_name = NULL) {

	if (!empty($album_id) && !empty($video_file_name)) {

	  $this->album_model->add_photo($album_id, $video_file_name, 2);

	  echo $video_file_name;
	}
  }

}

?>